#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os,sys,re
from wwpy.util.Time import Time
from wwpy.util.Wrapper import looper
from wwpy.db.DBI import DBI,dbc
from wwpy.io.Source import Source

class PostInfo(object):
    
    def __init__(self,date=Time.yesterday(''),path='.'):
        self.date=date
        self.path=path
        self.sep='|'
        self.info={}
        
    @property
    def log_path(self):
        path='%s%slog' % (self.path,os.sep)
        if not os.path.exists(path):
            os.mkdir(path)
        return path
    
    @property
    def output_path(self):
        path='%s%spost_info' % (self.path,os.sep)
        if not os.path.exists(path):
            os.mkdir(path)
        return path
    
    @property
    def post_file(self):
        return '%s%spost_info_%s' % (self.output_path,os.sep,self.date)
    
    def run(self):
        from wwpy.dmc.agg.DMS import DMS,Field,Src,Hih
        dms=DMS(
            select=[Field(name='date',value=self.date,type='date',comment='统计日期'),
                    Field(name='user_id',value='s.user_id',type='bigint(20)',comment='用户ID'),
                    Field(name='city',value='l.city1',type='varchar(30)',comment='城市'),
                    Field(name='cate1',value='c.cate1',type='varchar(30)',comment='一级类别'),
                    Field(name='cate2',value='c.cate2',type='varchar(30)',comment='二级类别'),
                    Field(name='post_count',value='s.info_id',func='count',where={'s.operation':lambda oper:oper=='0'},comment='发帖数'),
                    Field(name='modify_post_count',value='s.info_id',func='count',where={'s.operation':lambda oper:oper=='1'},comment='修改帖子数'),
                    Field(name='delete_post_count',value='s.info_id',func='count',where={'s.state':lambda state:state=='0'},comment='删除帖子数'),
                ],
            from_=[Src(name='s',value="%s/*%s*" % (self.log_path,self.date),sep=self.sep,fields='info_id,user_id,local_id,cate_id,add_time,operation,state,ip,phone'.split(',')),
                   Src(name='l',value="SELECT local_id,city1 FROM db_base_dict.belong_local",line_func=lambda row:[str(field) for field in row]),
                   Src(name='c',value="SELECT cate_id,cate1,cate2 FROM db_base_dict.belong_cate",line_func=lambda row:[str(field) for field in row]),
                   ],
            join=[Hih(src='l',on='l.local_id=s.local_id'),
                  Hih(src='c',on='c.cate_id=s.cate_id'),
                ],
            group=['user_id',
                   'city',
                   'cate1',
                   'cate2',
                   ],
        )
        dms.sep(self.sep)
        dms.file(self.post_file)
        dms.store()
        dms.load('post_info',db='db_58_ib',where=dict(stat_date=self.date),options={'ENGINE':'BRIGHTHOUSE'})

class PostInfo2(object):
    
    def __init__(self,date=Time.yesterday(''),path='.'):
        self.date=Time.date_format(date)
        self.mini_date=Time.date_format(date,'')
        self.path=path
        self.sep='|'
        self.info={}
        
    @property
    def log_path(self):
        path='%s%slog' % (self.path,os.sep)
        if not os.path.exists(path):
            os.mkdir(path)
        return path
    
    @property
    def output_path(self):
        path='%s%spost_info' % (self.path,os.sep)
        if not os.path.exists(path):
            os.mkdir(path)
        return path
    
    @property
    def post_file(self):
        return '%s%spost_info_%s' % (self.output_path,os.sep,self.mini_date)
    
    def get_local_dict(self):
        local_dict={}
        s=Source("SELECT local_id,city1,city2,city3 FROM db_base_dict.belong_local")
        for row in s.read():
            local_dict[row[0]]=row[1:]
        return local_dict
    
    def get_cate_dict(self):
        cate_dict={}
        s=Source("SELECT cate_id,cate1,cate2,cate3,cate4 FROM db_base_dict.belong_cate")
        for row in s.read():
            cate_dict[row[0]]=row[1:]
        return cate_dict
    
    def get_user_dict(self):
        user_dict={}
        s=Source("SELECT distinct user_id FROM db_58_stat.order_info where product_type=10 and date(service_begin_time)<='%s' and date(service_end_time)<='%s'" % (self.date,self.date))
        for row in s.read():
            user_dict[row[0]]=1
        return user_dict
    
    def process(self):
        ld=self.get_local_dict()
        cd=self.get_cate_dict()
        ud=self.get_user_dict()
        s=Source("%s/*%s*" % (self.log_path,self.mini_date))
        info={}
        for line in s.read():
            info_id,user_id,local_id,cate_id,add_time,operation,state,ip,phone=line.split(self.sep)
            city1,city2,city3=ld.get(local_id,('','',''))
            cate1,cate2,cate3,cate4=cd.get(cate_id,('','','',''))
            is_vip=ud.get(user_id,0)
            key=(user_id,is_vip,city1,city2,city3,cate1,cate2,cate3,cate4)
            info.setdefault(key,{})
            if operation=='0':
                info[key].setdefault('post_count',0)
                info[key]['post_count']+=1
            elif operation=='1':
                info[key].setdefault('modify_post_count',0)
                info[key]['modify_post_count']+=1
            if state=='0':
                info[key].setdefault('delete_post_count',0)
                info[key]['delete_post_count']+=1
        f=open(self.post_file,'w')
        for key in info:
            posts=map(lambda name:info[key].get(name,0),'post_count,modify_post_count,delete_post_count'.split(','))
            f.write(self.sep.join(str(field) for field in [self.mini_date]+list(key)+posts)+'\n')
        f.close()
        
    def load(self):
        table_fields=[
            "stat_date date COMMENT '统计日期'",
            "user_id bigint(20) COMMENT '用户ID'",
            "is_vip smallint(2) COMMENT '是否是会员 1:是 0:否'",
            "city1 varchar(30) COMMENT '一级城市'",
            "city2 varchar(30) COMMENT '二级城市'",
            "city3 varchar(30) COMMENT '三级城市'",
            "cate1 varchar(30) COMMENT '一级类别'",
            "cate2 varchar(30) COMMENT '二级类别'",
            "cate3 varchar(30) COMMENT '三级类别'",
            "cate4 varchar(30) COMMENT '四级类别'",
            "post_count int(11) COMMENT '发帖数'",
            "modify_post_count int(11) COMMENT '修改帖子数'",
            "delete_post_count int(11) COMMENT '删除帖子数'",
        ]
        dbi=DBI(**dbc['db_58_ib'])
        dbi.save(self.post_file,'post_info',table_fields=table_fields,sep=self.sep,is_update=False,options={'ENGINE':'BRIGHTHOUSE'},where=dict(stat_date=self.mini_date))
        dbi.close()
        
    def run(self):
        self.process()
        self.load()
        
@looper('DAY')
def main(date):
    path='/work2/opt/analytic/wuba/user/post'
    pi=PostInfo2(date,path)
    pi.run()
    
if __name__ == '__main__':
    main()
